package com.cln.treewalker;

import junit.framework.TestCase;

public class TreeWalkerTest extends TestCase {

	// 对T进行前序遍历是先访问树根n,然后依次前序遍历T1,T2,..,Tk。
	// 对T进行中序遍历是先中序遍历T1，然后访问树根n，接着依次对T2,T2,..,Tk进行中序遍历。
	// 对T进行后序遍历是先依次对T1,T2,..,Tk进行后序遍历，最后访问树根n
	public void testSimpleTreeWalk() {

		IVisitor preOrderTraverser= new TreePreOrderTraverser();
		IVisitor inOrderTraverser= new TreeInOrderTraverser();
		IVisitor postOrderTraverser= new TreePostOrderTraverser();
		
		TreeNode a= new TreeNode("a");
		TreeNode b= new TreeNode("b");
		TreeNode c= new TreeNode("c");
		a.setLeftChild(b);
		a.setRightChild(c);
		TreeNode d= new TreeNode("d");
		TreeNode e= new TreeNode("e");
		b.setLeftChild(d);
		b.setRightChild(e);
		
		a.accept(preOrderTraverser);
		assertEquals("abdec",preOrderTraverser.getResult().toString());
		
		a.accept(inOrderTraverser);
		assertEquals("dbeac",inOrderTraverser.getResult().toString());
		
		a.accept(postOrderTraverser);
		assertEquals("debca",postOrderTraverser.getResult().toString());
	}
	public void testSpecialTreeWalk() {

		IVisitor preOrderTraverser= new TreePreOrderTraverser();
		IVisitor inOrderTraverser= new TreeInOrderTraverser();
		IVisitor postOrderTraverser= new TreePostOrderTraverser();
		
		TreeNode a= new TreeNode("a");
		TreeNode b= new TreeNode("b");
		TreeNode c= new TreeNode("c");
		a.setLeftChild(b);
		a.setRightChild(c);
		TreeNode d= new TreeNode("d");
		TreeNode e= new TreeNode("e");
		b.setLeftChild(d);
		b.setRightChild(e);
		
		TreeNode s = new SpecialTreeNode("s");
		e.setLeftChild(s);
		TreeNode f= new TreeNode("f");
		s.setRightChild(f);
		a.accept(preOrderTraverser);
		assertEquals("abdefc",preOrderTraverser.getResult().toString());
		
		a.accept(inOrderTraverser);
		assertFalse("dbeac".equals(inOrderTraverser.getResult().toString()));
		assertEquals("dbsfeac",inOrderTraverser.getResult().toString());
		
		a.accept(postOrderTraverser);
		assertFalse("debca".equals(postOrderTraverser.getResult().toString()));
		assertEquals("dfsebca",postOrderTraverser.getResult().toString());
	}
}
